﻿using System.Diagnostics;
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;

namespace UtilLib
{
    public class EventLogger
    {
        static EventLogger()
        {
            var hierarchy = (Hierarchy)LogManager.GetRepository();

            var patternLayout = new PatternLayout
            {
                ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"
            };
            patternLayout.ActivateOptions();

            var roller = new RollingFileAppender
            {
                AppendToFile = false,
                File = @"Logs\EventLog.log",
                Layout = patternLayout,
                MaxSizeRollBackups = 5,
                MaximumFileSize = "100MB",
                RollingStyle = RollingFileAppender.RollingMode.Date,
                StaticLogFileName = true
            };

            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);

            var memory = new MemoryAppender();
            memory.ActivateOptions();
            hierarchy.Root.AddAppender(memory);

            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;
        }

        private static ILog _logger;

        private static ILog Logger
        {
            get
            {
                if (_logger == null)
                {
                    var name = Process.GetCurrentProcess().ProcessName;
                    _logger = LogManager.GetLogger(name);
                }

                return _logger;
            }
        }

        public static void LogError(object error)
        {
            Logger.Error(error);
        }

        public static void LogInfo(object info)
        {
            Logger.Info(info);
        }
    }
}
